#include<iostream>
#include<pthread.h>
#include<unistd.h>
#include<vector>
#include<unistd.h>
#include<string>

using namespace std;

class Request{
public:
    Request(int start,int end,string name)
    :_start(start)
    ,_end(end)
    ,_name(name)
    {}

    int _Sum()
    {
        int sum=0;
        for(int i=_start;i<=_end;i++)
        {
            cout<<"name:"<<_name<<" running:"<<i<<endl;
            sum+=i;
            usleep(100000);
        }

        return sum;
    }

private:
    int _start;
    int _end;
    string _name;
};

class Response{
public:
    Response(int sum,int exitcode)
    :_sum(sum)
    ,_exitcode(exitcode)
    {}
public:
    int _sum;
    int _exitcode;
};

void* sumCount(void*arg)
{
    Request* r=static_cast<Request*> (arg);
    int ret=r->_Sum();
    Response* rp=new Response(ret,0);

    return rp;
}

int main()
{
    pthread_t tid;
    vector<pthread_t> tids;
    for(int i=0;i<10;i++)
    {
        string s="pthread-"+to_string(i+1);
        Request* r=new Request((i)*10+1,(i+1)*10,s);
        pthread_create(&tid,nullptr,sumCount,r);
        tids.push_back(tid);
        usleep(10000);
    }

    vector<Response*> rets;
    void* ret;
    for(int i=0;i<tids.size();i++)
    {
        pthread_join(tids[i],&ret);
        rets.push_back(static_cast<Response*> (ret));
    }

    int sum=0;
    for(int i=0;i<rets.size();i++)
    {
        sum+=rets[i]->_sum;
        printf("ret[%d]->_sum:%d,ret[%d]->_exitcode:%d\n",i,rets[i]->_sum,i,rets[i]->_exitcode);
        delete rets[i];
    }

    cout<<"sum:"<<sum<<endl;


    return 0;

}